package com.newlink.building.login.common.widget

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.newlink.building.login.R
import com.newlink.building.login.common.Module_Login_CheckUrlBean
import com.newlink.building.common_base.ext.showToast
import com.newlink.building.common_base.utils.RegexUtils
import com.newlink.building.common_base.widget.CustomInputDialog
import com.newlink.building.common_base.widget.SpaceItemDecoration

class Module_Login_EnvChoiceDialog(context: Context) : BottomSheetDialog(context, R.style.BottomSheetDialog) {

    private var mTitle: String = ""
    private var mCheckBeans: MutableList<Module_Login_CheckUrlBean> = mutableListOf()
    private var mMessage: String = ""
    private var mMessageHint: String = ""
    private var mNegative: String = ""
    private var mPositive: String = ""
    private var negativeListener: OnClickListener? = null
    private var positiveListener: OnClickListener? = null
    private var mPosition: Int = 0
    private var mAdapter: Module_Login_EnvChoiceAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_env_choice)
        window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        mAdapter = Module_Login_EnvChoiceAdapter(mCheckBeans)
        recyclerView?.adapter = mAdapter
        recyclerView?.addItemDecoration(SpaceItemDecoration(context))
        for ((index, item) in mCheckBeans.withIndex()) {
            item.isChecked = index == mPosition
        }
        mAdapter?.setOnItemClickListener { adapter, _, position ->
            mPosition = position
            if (mCheckBeans[position].type == 1) {
                showCustomInputDialog(
                    mCheckBeans[position],
                    context.getString(R.string.login_region_custom_fcms_title)
                )
            } else {
                positiveListener?.onClick(mCheckBeans[mPosition], mPosition)
            }
        }
//        tv_confirm.setOnClickListener {
//            positiveListener?.onClick(mCheckBeans[mPosition], mPosition)
//        }
//        tv_cancel.setOnClickListener {
//            negativeListener?.onClick(mCheckBeans[mPosition], mPosition)
//        }
    }


    fun setTitle(title: String): Module_Login_EnvChoiceDialog {
        mTitle = title
        return this
    }

    fun setCheckBeans(beans: MutableList<Module_Login_CheckUrlBean>): Module_Login_EnvChoiceDialog {
        mCheckBeans = beans
        return this
    }

    fun setSelectedPostion(position: Int): Module_Login_EnvChoiceDialog {
        mPosition = position
        return this
    }

    fun setMessage(message: String): Module_Login_EnvChoiceDialog {
        mMessage = message
        return this
    }

    fun setMessageHint(messageHint: String): Module_Login_EnvChoiceDialog {
        mMessageHint = messageHint
        return this
    }

    fun setNegativeButton(
        listener: OnClickListener?,
    ): Module_Login_EnvChoiceDialog {
        if (listener == null) {
            this.mNegative = ""
        } else {
            this.mNegative = context.resources.getString(R.string.cancel_dialog_custom)
            this.negativeListener = listener
        }
        return this
    }

    fun setNegativeButton(
        text: String = context.resources.getString(R.string.cancel_dialog_custom),
        listener: OnClickListener,
    ): Module_Login_EnvChoiceDialog {
        if (TextUtils.isEmpty(text)) {
            this.mNegative = ""
        } else {
            this.mNegative = text
            this.negativeListener = listener
        }

        return this
    }

    fun setPositiveButton(
        listener: OnClickListener?,
    ): Module_Login_EnvChoiceDialog {
        if (listener == null) {
            this.mPositive = ""
        } else {
            this.mPositive = context.resources.getString(R.string.confirm_dialog_custom)
            this.positiveListener = listener
        }
        return this
    }

    fun setPositiveButton(
        text: String = context.resources.getString(R.string.confirm_dialog_custom),
        listener: OnClickListener,
    ): Module_Login_EnvChoiceDialog {
        if (TextUtils.isEmpty(text)) {
            this.mPositive = ""
        } else {
            this.mPositive = text
            this.positiveListener = listener
        }

        return this
    }

    interface OnClickListener {
        fun onClick(bean: Module_Login_CheckUrlBean, position: Int)
    }

    private fun showCustomInputDialog(
        bean: Module_Login_CheckUrlBean,
        title: String,
        inputType: Int = EditorInfo.TYPE_CLASS_TEXT
    ) {
        val customInputDialog = CustomInputDialog(context)
        customInputDialog.setTitle(title).setText(bean.url).setInputType(inputType)
            .setNegativeButton(listener = object : CustomInputDialog.OnClickListener {
                override fun onClick(type: Int, inputText: String) {
                    customInputDialog.dismiss()
                    this@Module_Login_EnvChoiceDialog.dismiss()
                }

            }).setPositiveButton(listener = object : CustomInputDialog.OnClickListener {
                override fun onClick(type: Int, inputText: String) {
                    if (inputText.isNullOrEmpty() || !RegexUtils.checkURL(inputText)) {
                        showToast(context.getString(R.string.login_region_custom_address_invalid))
                        return
                    }
                    bean.url = inputText
                    mAdapter?.notifyDataSetChanged()
                    customInputDialog.dismiss()
                    positiveListener?.onClick(bean, mPosition)
                }
            })
        customInputDialog.show()
    }
}